package com.foreveredu.propms.web.struts.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.Message;
import com.foreveredu.propms.domain.News;
import com.foreveredu.propms.domain.Owner;
import com.foreveredu.propms.domain.PageModel;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.web.struts.actionform.MessageActionForm;
import com.foreveredu.propms.web.struts.actionform.NewsActionForm;
import com.foreveredu.propms.web.struts.actionform.OwnerActionForm;

public class MessageAdminAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	public ActionForward findMessageById(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		MessageActionForm messageform = (MessageActionForm) form;
		Message message = managementService
				.findMessageById(messageform.getId());
		request.setAttribute("message", message);
		return mapping.findForward("findMessageById");
	}

	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int offset = 0;
		String pageOffset = request.getParameter("pager.offset");// 获得翻页参数，即下一开始的纪录位置
		if (pageOffset != null && !pageOffset.equals("")) {
			offset = Integer.parseInt(pageOffset);
		}
		if (offset < 0) {
			return mapping.findForward("messageList");
		}
		PageModel pm = managementService.findMessageList(offset);
		request.setAttribute("pm", pm);
		return mapping.findForward("messageList");
	}

	public ActionForward updateMessageState(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		MessageActionForm messageform = (MessageActionForm) form;
		Message message = managementService
				.findMessageById(messageform.getId());

		if (message.getState() == 0) {
			message.setState(1);
			managementService.updateMessage(message);
			return mapping.findForward("updateMessageState");
		}
		if (message.getState() == 1) {
			message.setState(0);
			managementService.updateMessage(message);
			return mapping.findForward("updateMessageState");
		}
		return null;
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MessageActionForm messageform = (MessageActionForm) form;
		Message message = managementService
				.findMessageById(messageform.getId());
		managementService.deleteMessage(message);
		return mapping.findForward("delete");
	}
}
